package mornd;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author: mornd
 * @dateTime: 2023/5/5 - 16:13
 */

//@Scope("prototype")
@RestController
public class MyController {

    // 单例 bean 中如果要注入其它范围的 bean 需要添加 @Lazy 注解
    @Lazy
    @Autowired
    private BeanForRequest beanForRequest;
    @Lazy
    @Autowired
    private BeanForSession beanForSession;
    @Lazy
    @Autowired
    private BeanForApplication beanForApplication;

    @GetMapping(value = "/test", produces = "text/html")
    public String test(HttpServletRequest request, HttpSession session) {
        String result = this + "<br/>" + beanForRequest + "<br/>" + beanForSession + "<br/>" + beanForApplication;
        return result;
    }

    @GetMapping("/test2")
    public String test2(HttpSession session) {
        System.out.println(session.getId());
        // 手动销毁 session
        session.invalidate();
        return session.getId();
    }
}
